package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.RepairAliYunCallErrorNumberDTO;
import com.xbongbong.paas.pojo.vo.RepairAliYunCallErrorNumberVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.RepairAliYunCallService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author WuHB
 * @date 2022/09/01 16:13
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/aliYunCall")
public class RepairAliYunCallController {

    @Resource
    private RepairAliYunCallService repairAliYunCallService;

    @RequestMapping(value = "/repairAliYunCallErrorNumber", method = RequestMethod.POST)
    public String listErrorCallId(@RequestBody @Valid RepairAliYunCallErrorNumberDTO callRecordDTO, BindingResult bindingResult) {
        XbbResponse<RepairAliYunCallErrorNumberVO> response;
        if (bindingResult.hasErrors()) {
            String errMsg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RepairAliYunCallErrorNumberVO scriptVO = repairAliYunCallService.repairAliYunCallErrorNumber(callRecordDTO);
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
